package com.hellpro.lolbox.mine;


import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.mine.presenter.MinePresenter;
import com.hellpro.lolbox.mine.view.IMineFragment;
import com.hellpro.lolbox.mine.view.LoginSettingActivity;
import com.hellpro.lolbox.mine.view.UnLoginSettingActivity;
import com.hellpro.lolbox.utils.UIManger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

import static android.content.Context.MODE_PRIVATE;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements IMineFragment {


    @BindView(R.id.main_ll_login)
    LinearLayout mainLlLogin;
    @BindView(R.id.main_login_iv_setting)
    ImageView mainLoginIvSetting;
    @BindView(R.id.main_login_btn_show)
    Button mainLoginBtnShow;
    @BindView(R.id.mine_ll_show)
    LinearLayout mineLlShow;
    @BindView(R.id.main_register_fans)
    TextView mainRegisterFans;
    @BindView(R.id.main_logined_info)
    LinearLayout mainLoginedInfo;
    @BindView(R.id.main_logined_setting)
    ImageView mainLoginedSetting;
    @BindView(R.id.main_register_show)
    Button mainRegisterShow;
    @BindView(R.id.mine_logined_friend)
    RadioButton mineLoginedFriend;
    @BindView(R.id.mine_logined_chat)
    RadioButton mineLoginedChat;
    @BindView(R.id.mine_logined_near)
    RadioButton mineLoginedNear;
    @BindView(R.id.mine_logined_addFriend)
    RadioButton mineLoginedAddFriend;
    @BindView(R.id.mine_logined_meiLi)
    LinearLayout mineLoginedMeiLi;
    @BindView(R.id.mine_logined_rl)
    RelativeLayout mineLoginedRl;
    @BindView(R.id.mine_ll_logined)
    LinearLayout mineLlLogined;
    @BindView(R.id.mine_show_lv)
    PullToRefreshListView mineShowLv;
    @BindView(R.id.main_loginer_name)
    TextView mainLoginerName;


    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/24
     * 备注:判断是否登录
     */

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences sp = getActivity().getSharedPreferences("isLogin", MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin", false);
        if (isLogin) {//是否登录
            //登录过的界面
            mineLlLogined.setVisibility(View.VISIBLE);
            mineLlShow.setVisibility(View.GONE);
        } else {
            //未登录的界面
            mineLlLogined.setVisibility(View.GONE);
            mineLlShow.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        MinePresenter presenter = new MinePresenter(this);
        presenter.startLogin();
        presenter.setLvAdapter();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getUserInfo(BmobUser user) {
        mainLoginerName.setText(user.getNickName());
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public LinearLayout getLoginLlCOntent() {
        return mainLlLogin;
    }

    @Override
    public ImageView getLoginSetting() {
        return mainLoginIvSetting;
    }

    @Override
    public Context getMineContext() {
        return getActivity();
    }

    @Override
    public PullToRefreshListView getMineLV() {
        return mineShowLv;
    }

    @Override
    public String[] getStringConetxt() {
        return getResources().getStringArray(R.array.mine_context);
    }

    @Override
    public int[] getIntImgs() {
        return new int[]{R.mipmap.icon_celebrity,R.mipmap.icon_charm_rank,R.mipmap.icon_discover_kaihei,R.mipmap.icon_discover_nearby,R.mipmap.icon_erea_rank};
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.main_logined_setting, R.id.main_login_iv_setting})
    public void onClick(View view) {
        switch (view.getId()) {
            //登录后的设置
            case R.id.main_logined_setting:
                UIManger.startToActivity(getActivity(), LoginSettingActivity.class);
                break;
            case R.id.main_login_iv_setting:
                UIManger.startToActivity(getActivity(), UnLoginSettingActivity.class);
                break;
        }
    }

}
